package com.igeek.boot.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.entity.Assets;
import com.igeek.boot.entity.ContractApprovals;
import com.igeek.boot.entity.ContractTemplates;
import com.igeek.boot.entity.Contracts;
import com.igeek.boot.result.Result;
import com.igeek.boot.service.IContractApprovals;
import com.igeek.boot.service.IContractsService;
import com.igeek.boot.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jwj
 * @since 2023-12-26
 */
@Slf4j
@RestController
@RequestMapping("/wisdom_factory/contracts")
@Api(tags = "合同表接口")
public class ContractsController {

    @Autowired
    private IContractsService contractsService;

    @Autowired
    private IContractApprovals approvals;


    @PostMapping("/upload")
    @ApiOperation("上传合同模版")
    public Result upLoad(MultipartFile file) throws IOException {
        String fileName = contractsService.upload(file);
        if(StringUtils.hasLength(fileName)){

            return new Result(true,"上传成功",fileName);
        }
        return new Result(false,"上传失败");
    }
    @ApiOperation("合同发起")
    @PostMapping("/addContract")
    public Result addContract(@RequestBody Contracts contracts){
        boolean flag = contractsService.addContract(contracts);
        if(flag){
            boolean flag1 = approvals.addApproval(contracts);
            if(flag1){
                return new Result(true,"合同发起成功，请等待审批");
            }
            return new Result(true,"审批失败");

        }
        return new Result(false,"合同发起失败");
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/findPage")
    public Result findPage(@ApiParam("分页条件") PageQueryVO pageQueryVO){
        Page<Contracts> page = contractsService.findPage(pageQueryVO);
        if(!ObjectUtils.isEmpty(page)) return new Result(true,"分页合同查询成功",page);
        return new Result(false,"分页合同查询失败");
    }

    //删除合同
    @ApiOperation("根据id删除合同")
    @DeleteMapping("/delContract/{id}")
    public Result delContract(@PathVariable Integer id){
        boolean flag = approvals.delContract(id);
        boolean page = contractsService.delContract(id);
        if(page){

            return new Result(true,"删除成功");
        }
        return new Result(false,"删除失败");
    }

    @ApiOperation("修改合同状态")
    @GetMapping("/updateStatus")
    public Result updateStatus(Integer id,String status){
        boolean flag = contractsService.updateStatus(id,status);
        log.info("id为{}的合同开始审批",id);
        if (flag) return new Result(true,"状态修改成功");
        return new Result(false,"状态修改失败");
    }

    @ApiOperation("修改审批状态")
    @GetMapping("/updateApproval")
    public Result updateApproval(Integer id,String status){
        boolean flag = approvals.updateApproval(id,status);
        log.info("id为{}的合同开始审批",id);
        if (flag) return new Result(true,"审批成功");
        return new Result(false,"审批失败");
    }

    @ApiOperation("修改审批信息")
    @PostMapping("/updateComment")
    public Result updateApproval(@RequestBody ContractApprovals approval){
        boolean flag = approvals.updateComment(approval);
        log.info("合同审批不通过{}",approval.getContractID());
        if (flag) return new Result(true,"审批成功");
        return new Result(false,"审批失败");
    }

    //查询审批状态并携带合同信息
    @ApiOperation("分页查询审批状态")
    @GetMapping("findPageByApp")
    public Result findPageByApp(PageQueryVO pageQueryVO){
        Page<ContractApprovals> page = approvals.findPageByApp(pageQueryVO);
        return new Result(true,"查询成功",page);

    }

    @ApiOperation("首页数据展示")
    @GetMapping("findPageByStatus")
    public Result findPageByStatus(PageQueryVO pageQueryVO){
        Page<Contracts> page = contractsService.findPageByStatus(pageQueryVO);
        return new Result(true,"查询成功",page);

    }

    //获取饼图数据
    @ApiOperation("饼状图显示")
    @GetMapping("findPie")
    public Result findPie(){
        Map<String,Integer> map = contractsService.findPie();
        return new Result(true,"查询成功",map);
    }
}
